package cn.highedu.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {

    Logger logger = LoggerFactory.getLogger(DemoController.class);

    @RequestMapping("/demo")
    public String demo(Model model){
        // 在model中添加属性，属性名为message，属性值为Hello World!
        // 属性被传递到视图中，视图可以使用属性值 ${message} 显示
        model.addAttribute("message", "Hello World!");
        return "/demo.html";
    }

    /**
     * 测试使用 @RequestHeader 获取请求头
     */
    @GetMapping("/test-headers")
    @ResponseBody
    public String test(@RequestHeader(HttpHeaders.USER_AGENT) String userAgent,
                       @RequestHeader(required = false, name = HttpHeaders.AUTHORIZATION) String token){
        logger.debug("User-Agent: {}", userAgent);
        // 获取认证头
        logger.debug("token : {}", token);
        return "OK";
    }

}
